package cn.daniellee.plugin.rs.command;

import cn.daniellee.plugin.rs.RestrictedShop;
import cn.daniellee.plugin.rs.core.ShopCore;
import cn.daniellee.plugin.rs.menu.*;
import cn.daniellee.plugin.rs.model.PlayerData;
import cn.daniellee.plugin.rs.model.Shop;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Collections;

public class ShopCommand implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
		// 玩家无参数使用则打开菜单
		if (strings.length > 0) {
			// 显示商店列表
			if (strings[0].equalsIgnoreCase("list") && commandSender.hasPermission("rshop.command.list")) {
				commandSender.sendMessage((RestrictedShop.getInstance().getPrefix() + RestrictedShop.getInstance().getConfig().getString("message.shop-list-prefix", "&eShop list: &b")).replace("&", "§") + StringUtils.join(Collections.list(ShopCore.SHOPS.keys()), ", "));
			// 重置玩家的商店
			} else if (strings[0].equalsIgnoreCase("reset") && strings.length > 2 && commandSender.hasPermission("rshop.command.reset")) {
				RestrictedShop.getInstance().getStorage().deletePlayerData(strings[1], strings[2]);
				commandSender.sendMessage((RestrictedShop.getInstance().getPrefix() + RestrictedShop.getInstance().getConfig().getString("message.reset-success", "&eThe target player's shop has been reset.")).replace("&", "§"));
			// 重载插件配置文件
			} else if (strings[0].equalsIgnoreCase("reload") && commandSender.hasPermission("rshop.command.reload")) {
				RestrictedShop.getInstance().loadConfig();
				commandSender.sendMessage((RestrictedShop.getInstance().getPrefix() + RestrictedShop.getInstance().getConfig().getString("message.reload-success", "&eConfiguration reloading succeeded.")).replace("&", "§"));
			// 显示商店GUI
			} else if (commandSender instanceof Player) {
				Player player = (Player) commandSender;
				// 查找商店信息
				Shop shop = ShopCore.SHOPS.get(strings[0]);
				if (shop != null) {
					// 获取存储中的玩家信息
					PlayerData playerData = RestrictedShop.getInstance().getStorage().getPlayerData(player.getName(), strings[0]);
					// 核对校验和和生成时间
					if (playerData == null || !shop.getCheckSum().equals(playerData.getCheckSum()) || playerData.getGenerateAt() - System.currentTimeMillis() > shop.getRefreshInterval()) {
						// 生成新的PlayerData
						playerData = ShopCore.GeneratePlayerDataByShop((Player) commandSender, shop);
					}
					// 写入本地缓存
					ShopCore.TRADING.put(player.getName(), playerData);
					// 打开菜单
					player.openInventory(ShopMenu.generate(shop, playerData));
				} else sendHelp(commandSender);
			} else sendHelp(commandSender);
		} else sendHelp(commandSender);
		return true;
	}

	private void sendHelp(CommandSender commandSender) {
		commandSender.sendMessage(("&m&6---&m&a--------&3 : " + RestrictedShop.getInstance().getConfig().getString("prompt-prefix", "&6RestrictedShop") + "&3 : &m&a--------&m&6---").replace("&", "§"));

		String openText =  RestrictedShop.getInstance().getConfig().getString("help.open", "&eOpen a shop.").replace("&", "§");
		TextComponent openHelp = new TextComponent("/rs <shop>  " + openText);
		openHelp.setColor(ChatColor.GRAY);
		openHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/rs "));
		openHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(openText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(openHelp);

		String listText =  RestrictedShop.getInstance().getConfig().getString("help.list", "&eShow shop list.").replace("&", "§");
		TextComponent listHelp = new TextComponent("/rs list  " + listText);
		listHelp.setColor(ChatColor.GRAY);
		listHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/rs list"));
		listHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(listText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(listHelp);

		String resetText =  RestrictedShop.getInstance().getConfig().getString("help.reset", "&eReset some player's shop.").replace("&", "§");
		TextComponent resetHelp = new TextComponent("/rs reset <player> <shop>  " + resetText);
		resetHelp.setColor(ChatColor.GRAY);
		resetHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/rs reset "));
		resetHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(resetText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(resetHelp);

		String reloadText = RestrictedShop.getInstance().getConfig().getString("help.reload", "Reload configuration.").replace("&", "§");
		TextComponent reloadHelp = new TextComponent("/rs reload  " + reloadText);
		reloadHelp.setColor(ChatColor.GRAY);
		reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/rs reload"));
		reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(reloadText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(reloadHelp);
	}

}
